<template>
	<view class="route_details">
		<xbd-pages-content title="title">
		<template #content>
			<view class="content">
				<xbd-null-list v-if="!list.length"></xbd-null-list>
				<view class="head" v-for="(item,index) in list" :key="index">
					<view class="state">
						下单时间：{{item.create_time | timeFormat('YYYY-MM-DD HH:mm:ss') }}
						<view class="right" v-if="item.is_in_car === false">
							<view class="button" @click="cancel(item.order_id)">
								取消
							</view>
							<view class="button" @click ="intoCar(item.order_id,item.car_id,item.car_name)">
								入车
							</view>
						</view>
						<view class="right2" v-else>
							<view class="button">
								已入车
							</view>
						</view>
					</view>
					<view class="type">
						<view class="left">
							电话
						</view>
						<view class="right">
							{{item.phone || '-' }}
						</view>
					</view>
					<view class="type">
						<view class="left">
							姓名
						</view>
						<view class="right">
							{{item.username || '-'}}
						</view>
					</view>
					<view class="type">
						<view class="left">
							收货站点
						</view>
						<view class="right">
							{{item.receiving_site || '-'}}
						</view>
					</view>
					<view class="type">
						<view class="left">
							店铺
						</view>
						<view class="right">
							{{item.shop || '-'}}
						</view>
					</view>
					<view class="type">
						<view class="left">
							配送车辆
						</view>
						<view class="right">
							{{item.car_name || '-'}}
						</view>
					</view>
					<view class="type">
						<view class="left">
							格口
						</view>
						<view class="right">
							{{item.cabinet || '-'}}
						</view>
					</view>
					<view class="type">
						<view class="left">
							订单编号
						</view>
						<view class="right">
							{{item.order_id || '-'}}
						</view>
					</view>
				</view>
				<xbd-popup :isShow="showDialog1" @dialogConfirm="dialogConfirm1" @dialogCancel="dialogCancel1"
					content="确定取消这个订单吗？" cancelText="取消" confirmText="确定">
				</xbd-popup>
				<xbd-popup :isShow="showDialog2" @dialogConfirm="dialogConfirm2" @dialogCancel="dialogCancel2"
					content="确定出发？" cancelText="取消" confirmText="确定">
				</xbd-popup>
				<view class="foot">
					<view class="two">
						<view class="button" @click="open()">
							出发
						</view>
					</view>
				</view>
			</view>
			
		</template>
		</xbd-pages-content>
	</view>
</template>

<script>
	import { DeliveryOrder } from '@/api/index.js';
	export default {
		data() {
			return {
				id:'',
				flag:true,
				title:'动态线路',
				showDialog1: false,
				showDialog2: false,
				list:[],
				info:{},
				cancel_id:'',
				sendOut:{
					car_id:'',
					route_id:this.id
				}
			};
		},
		methods: {
			intoCar(id,car_id,val){
				uni.navigateTo({
					url: `./intoCar?id=${id}&car=${car_id}&name=${val}`,
				})
			},
			// 发出操作
			open() {
				
				this.sendOut.car_id =  this.list[0].car_id
				this.sendOut.route_id =  this.id
				if(this.list.length === 0){
					this.$showToast('无发出订单！');
					
				}else if(!this.isOut){
					this.$showToast('全部订单入车后才可出发！');
				}else{
					this.showDialog2 = true
				}
			},
			cancel(id) {
				this.cancel_id = id
				this.info={
					order_id: id, //订单ID
					type: "1" //完成类型-0-待入车界面 1-动态路线详情界面
				}
				this.showDialog1 = true
			},
			//取消- 确认 操作
			dialogConfirm1() {
				
				this.cancelOrder()
				this.showDialog1 = false;
			},
			
			dialogCancel1() {
				this.showDialog1 = false;
			},
			// 发出操作
			//取消操作
			dialogConfirm2() {
				this.setOut();
				this.showDialog2 = false;
			},
			dialogCancel2() {
				this.showDialog2 = false;
			},
			// 初始化
			async init(){
				try {
					this.$showLoading('加载中');
					const res = await DeliveryOrder.getByRoute(this.id);
					this.list = res.data.data
					uni.hideLoading();
				} catch (err) {
					uni.hideLoading();
				}
			},
			ckeckEvent(val,index){
				this.list.forEach(item => {
					if (item.order_id === index) {
						item.is_in_car = true;
					}
				})
				this.sendOut.car_id = val
			},
			// 发出
			async setOut(){
					
				if( this.flag){
					try {
						this.flag = false
						this.$showLoading('加载中');
						const res = await DeliveryOrder.postCarSetout(this.sendOut);
							uni.hideLoading();
						if(res.success){
							
							this.flag = true
							uni.$emit('successOut',this.id)
							uni.navigateBack({
								delta: 1
							});
						}else{
							this.flag = true
							uni.showToast({
								title: res.msg,
								icon: 'none',
							})
						}
					} catch (err) {
						uni.hideLoading();
					}
				}
				
			},
			// 取消订单
			async cancelOrder(){
				if(this.flag){
				try {
					this.flag = false
						this.$showLoading('加载中');
						const res = await DeliveryOrder.postCancelOrder(this.info);
						if(res.success){
							this.flag = true
						}
						this.list = this.list.filter(item => {
							return item.order_id !== this.cancel_id
						})
						uni.hideLoading();
					} catch (err) {
						this.flag = true
						uni.showToast({
							title: err.msg,
							icon: 'none',
						})
						setTimeout(() => {
							uni.hideLoading();
						}, 1000)
					}
					}
			},
		},
		computed: {
			// 校验是否可以发出
			isOut(){
				let r = true
				this.list.forEach(item => {
					if (item.is_in_car === false) {
						return r = false;
					}
				})
				return r
			}
		},
		onLoad(res){
			this.id =res.id
			this.title = res.title
			uni.setNavigationBarTitle({
			    title: this.title
			});
			uni.$on('intoCar', this.ckeckEvent);
		},
		onShow(){
			this.init()
		},
		onUnload() {
			uni.$off('intoCar', this.ckeckEvent);
		},
	}
</script>

<style lang="scss" scoped>
	.content{
		box-sizing: border-box;
		padding-bottom: 160rpx;
		.head{
			padding: 0 40rpx 10rpx 40rpx;
			background-color: #FFFFFF;
			box-sizing: border-box;
			margin-bottom: 20rpx;
			.state{
				display: flex;
				align-items: center;
				justify-content: space-between;
				padding: 20rpx 0;
				border-bottom: 2rpx solid #E8E8E8;
				margin-bottom: 20rpx;
				white-space:normal;
				font-size: 24rpx;
				.right{
					display: flex;
					justify-content: space-between;
					width: 236rpx;
					.button{
						width: 108rpx;
						height: 44rpx;
						text-align: center;
						line-height: 40rpx;
						border-radius: 8rpx;
						border: 2rpx solid #457AFF;
						color: #457AFF;
						
					}
				}
				.right2{
					.button{
						width: 108rpx;
						height: 44rpx;
						text-align: center;
						line-height: 40rpx;
						border-radius: 8rpx;
						border: 2rpx solid #999999;
						color: #999999;
						
					}
				}
				
			}
		}
		.body{
			padding: 0 40rpx 10rpx 40rpx;
			background-color: #FFFFFF;
			box-sizing: border-box;
		}
		.type {
			
			display: flex;
			justify-content: space-between;
			font-size: 28rpx;
			margin-bottom: 20rpx;
		
			.left {
				color: #BBC0C9;
			}
		
			.right {
				color: #394954 10000%;
			}
		}
		.foot{
			width: 750rpx;
			position: fixed;
			bottom: 0;
			background-color: #FFFFFF;
			box-sizing: border-box;
			box-shadow: 0px -2px 6px 0px rgba(0, 0, 0, 0.1);
			font-size: 28rpx;
			.two{
				padding:0  160rpx;
				box-sizing: border-box;
				display: flex;
				justify-content: center;
				align-items: center;
				height: 100rpx;
				.button{
					width: 320rpx;
					height: 56rpx;
					line-height: 52rpx;
					text-align: center;
					box-sizing: border-box;
					border-radius: 8rpx;
					font-size: 24rpx;
					border: 2rpx solid #457AFF;
					color: #457AFF;
				}
			}
			
		}
	}

</style>
